/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unesp.rc.lcpjcontacts.utils;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.JTextComponent;


/**
 * The listener interface for receiving highlight events.
 * The class that is interested in processing a highlight
 * event implements this interface, and the object created
 * with that class is registered with a component using the
 * component's <code>addHighlightListener<code> method. When
 * the highlight event occurs, that object's appropriate
 * method is invoked.
 *
 * @author rafaelt
 */
public class HighlightListener implements DocumentListener {

    /** The txt comp list. */
    List<JTextComponent> txtCompList = new ArrayList<JTextComponent>();
    
    /** The jbutton. */
    JComponent jbutton = null;
    
    /** The default bg color list. */
    List<Color> defaultBgColorList = new ArrayList<Color>();
    
    /** The highlight bg color. */
    Color highlightBgColor = Color.decode("#F5FF8C");
    
    /**
     * Instantiates a new highlight listener.
     *
     * @param jtcList the jtc list
     * @param jbtn the jbtn
     */
    public HighlightListener(List<JTextComponent> jtcList, JComponent jbtn) {
        txtCompList = jtcList;
        jbutton = jbtn;
        
        int i = 0;
        for (JTextComponent txtComp: txtCompList){
            defaultBgColorList.add(txtComp.getBackground());
            // adiciona listener para o component especificado
            txtCompList.get(i).getDocument().addDocumentListener(this);
            i++;
        }
        

        // highlight se estiver vazio
        this.maybeHighlight();
    }    

    /* (non-Javadoc)
     * @see javax.swing.event.DocumentListener#insertUpdate(javax.swing.event.DocumentEvent)
     */
    public void insertUpdate(DocumentEvent e) {
        maybeHighlight();
    }

    /* (non-Javadoc)
     * @see javax.swing.event.DocumentListener#removeUpdate(javax.swing.event.DocumentEvent)
     */
    public void removeUpdate(DocumentEvent e) {
        maybeHighlight();
    }

    /* (non-Javadoc)
     * @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent)
     */
    public void changedUpdate(DocumentEvent e) {
        maybeHighlight();
    }

    /**
     * Maybe highlight.
     */
    private void maybeHighlight() {
        int i = 0;
        boolean enableButton = true;
        String txt = "";
        for (JTextComponent txtComp : txtCompList) {
            txt = txtComp.getText();
            txt = limparString(txt);
            
            if (txt.length() > 0) {
                // se o campo nao estiver vazio , troca para o look default
                txtComp.setBackground(defaultBgColorList.get(i));
            } else {
                // se o campo estiver vazio, highlight
                txtComp.setBackground(highlightBgColor);
                enableButton = false;
            }
            i++;
        }
        jbutton.setEnabled(enableButton);
    }

    /**
     * Limpar string.
     *
     * @param txt the txt
     * @return the string
     */
    private String limparString(String txt) {
    	txt = txt.replaceAll("\\/", "");
        txt = txt.replaceAll("\\(", "");
        txt = txt.replaceAll("\\)", "");
        txt = txt.replaceAll("\\.", "");
        txt = txt.replaceAll("_", "");
        txt = txt.replaceAll("-", "");
        txt = txt.trim();
        return txt;
    }
}